﻿/**
 * FansUnion-JS-String1.0
 *
 * QQ: 240370818
 * Email: LeiWen@FansUnion.cn
 * Date:2013.12.9
 * Copyright 2013-2014, leiwen,zhanshaolong
 */
/**
 *字符串工具类
 */ 
var StringUtil = (function(win, doc) {
	var StringUtil = win.StringUtil || {};
	/**
    *3位分割？正则没有看懂
    */
	StringUtil.format = function(str, ch, length){
		ch = ch || ",";
		length = length || 3;
		// (\d{1,3})(?=\d{3})(?:$|\.)))
		return ("" + str).replace(new RegExp("(\\d{1," + length + "})(?=(\\d{" + length + "})+(?:$|\\.))", "g"), "$1" + ch);
	};
	/**
    *构造一个length位的字符串，不足的字符串用ch顶替
	*str：原字符串
	*ch:补足位数用的字符，比如'a'
	*length:目标字符串的总长度
	*right:是否放在原有字符串的右边
    */
	StringUtil.fill = function(str, ch, length, right){
		var newStr = str+"";
		ch = ch || "0";
		length = length || 2;
		for(var i=0,length=length-newStr.length;i<length;i++){
			newStr = right ? newStr + ch : ch + newStr;
		}
		return newStr;
	};
	/**
    *生成一个由随机数字组成的字符串
	*length:字符串的长度
	*radix:进制，2,8,10等，默认为10
    */
	StringUtil.random = function(length, radix){
		var randomNumberStr = '';
        var	radix = radix || 10;
		try{
			randomNumberStr = Math.random().toString(radix).substr(2, length);
			while(randomNumberStr.length < length){
				randomNumberStr += Math.random().toString(radix).substr(2, length - randomNumberStr.length);
			}
		}catch(e){
		}
		return randomNumberStr;
	};
	/**
    *将字符串中的空白字符替换为指定的字符（默认为空） 
	*str:trim之前的原字符串
	*ch:需要替换的字符，比如'',则把相关字符替换为''
    */
	StringUtil.trim = function(str, ch){
		return str.replace(/^\s+|\s+$/, ch || '');
	};
	
	/**
	*删除字符串中的一个字符
	*idList:字符串集合
	*id:需要删除的字符串
	*seperator:字符串分隔符
	*/
	StringUtil.deleteId= function (idList,id,seperator) {

			var newIdList ="";
			if(seperator == null){
			  seperator=",";
			}
			if(idList !=""){
				var idArray = idList.split(seperator);
				var length = idArray.length;
				for(var index=0;index<length;index++){
					if(idArray[index] != id){
						newIdList += seperator+idArray[index];
					}
				}
			}
			//去掉第1个逗号','
			if (newIdList != "") {
				newIdList = newIdList.substring(1, newIdList.length);
			}
			
			return newIdList;
		};
		
	
	return StringUtil;
})(window, document);
